import { defineStore } from 'pinia';
import { CApplicationNode, CDeviceDataItem, CVariableDataItem } from 'stores/editorModels';
import { CUser, menuItem } from 'stores/model';

export const useCommonStore = defineStore('common', {
  state: () => ({
    leftDrawer: true,
    rightDrawer: false,
    pageRefresh: false,
    menuTab: false,

    application: <CApplicationNode> {        //应用信息
      projectId: 1,
      userServerAddr: '',
      adminServerAddr: '',
      scaServerAddr: '',
      ioServerAddr: '',
      ioServerFilePath: ''
    },

    menu: { list: <menuItem[]>[] },

    user: <CUser> {       //用户登录信息
      username: '',
      password: '',
      roleId: 0
    },
    token: '',            //token

    varTbl: <CVariableDataItem[]>[],  //变量数据库
    devTbl: <CDeviceDataItem[]>[]     //设备数据库
  }),

  getters: {

  },

  actions: {
    setLeftDrawer (s: boolean): void {
      this.leftDrawer = s;
    },

    setRightDrawer (s: boolean): void {
      this.rightDrawer = s;
    },

    setPageStatus (s: boolean): void {
      this.pageRefresh = s;
    },

    setMenuTab (s: boolean): void {
      this.menuTab = s;
    },

    setApplication (a: CApplicationNode): void {
      this.application.id = a.id;
      this.application.projectId = a.projectId;
      this.application.userServerAddr = a.userServerAddr;
      this.application.adminServerAddr = a.adminServerAddr;
      this.application.scaServerAddr = a.scaServerAddr;
      this.application.ioServerAddr = a.ioServerAddr;
      this.application.ioServerFilePath = a.ioServerFilePath;
    },

    setProject (id: number): void {
      this.application.projectId = id;
    },

    initMenus (): void {
      this.menu.list.length = 0;
      sessionStorage.removeItem('menu');
    },

    initUser (): void {
      this.user.username = '';
      this.user.password = '';
      this.user.roleId = 0;
      sessionStorage.removeItem('user');
    },

    initToken (): void {
      this.token = '';
      sessionStorage.removeItem('token');
    },

    getMenus (): void {
      let sMenu = sessionStorage.getItem('menu');
      if ((sMenu) && (sMenu !== '')) {
        this.menu = JSON.parse(sMenu);
      }
    },

    getToken (): void {
      let sToken = sessionStorage.getItem('token');
      if ((sToken) && (sToken !== '')) {
        this.token = JSON.parse(sToken);
      }
    },

    getUser (): void {
      let sUser = sessionStorage.getItem('user');
      if ((sUser) && (sUser !== '')) {
        this.user = JSON.parse(sUser);
      }
    },

    setMenus (menuList: menuItem[]): void {
      //深度拷贝
      menuList.forEach((menu) => {
        let item = {
          id: menu.id,
          pid: menu.pid,
          label: menu.label,
          value: menu.value,
          icon: menu.icon,
          url: menu.url,
          component: menu.component,
          menuType: menu.menuType,
          children: <menuItem[]>[]
        }
        menu.children.forEach((el) => {
          item.children.push(el);
        });

        this.menu.list.push(item);
      });

      sessionStorage.setItem('menu', JSON.stringify(this.menu));
    },

    setUser (user: CUser): void {
      this.user = user;
      sessionStorage.setItem('user', JSON.stringify(user));
    },

    setToken (token: string): void {
      this.token = token;
      sessionStorage.setItem('token', JSON.stringify(token));
    },

    initVariableTbl (): void {
      this.varTbl.length = 0;
    },

    addVariableTbl (id: number, data: number): void {
      let index = 0;
      index = this.varTbl.findIndex((item) => (item.varId === id));
      if (index === -1) {
        this.varTbl.push({
          varId: id,
          vData: data
        });
      } else {
        this.varTbl[index].vData = data;
      }
    },

    initDeviceTbl (): void {
      this.devTbl.length = 0;
    },

    addDeviceTbl (id: number, data: number): void {
      let index = 0;
      index = this.devTbl.findIndex((item) => (item.devId === id));
      if (index === -1) {
        this.devTbl.push({
          devId: id,
          status: data
        });
      } else {
        this.devTbl[index].status = data;
      }
    }
  }
})
